import { SpatialQueryAction, SpatialQueryActionTypes } from "../actions";


export enum SpatialQueryType {
    POINT = 'Point',
    LINE ='LineString',
    CIRCLE ='Circle',
    RECTANGLE = 'Rectangle',
    POLYGON = 'Polygon'
}

export interface SpatialQueryState {
    select: boolean,
    remove: number,
    clean: number,
    type: SpatialQueryType,
    removeUsable: boolean,
    cleanUsable: boolean
}

export const initialSpatialQueryState: SpatialQueryState = {
    select: true,
    remove: 0,
    clean: 0,
    type: null,
    removeUsable: false,
    cleanUsable: false
};

export function spatialQueryReducer(
    state: SpatialQueryState = initialSpatialQueryState,
    action: SpatialQueryAction
): SpatialQueryState {
    switch(action.type) {
        case SpatialQueryActionTypes.SPATIAL_QUERY_CIRCLE: {
            return {
                ...state,
                select: false,
                type: SpatialQueryType.CIRCLE
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_CLEAN: {
            return {
                ...state,
                clean: state.clean + 1
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_LINE: {
            return {
                ...state,
                select: false,
                type: SpatialQueryType.LINE
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_POINT: {
            return {
                ...state,
                select: false,
                type: SpatialQueryType.POINT
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_POLYGON: {
            return {
                ...state,
                select: false,
                type: SpatialQueryType.POLYGON
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_RECTANGLE: {
            return {
                ...state,
                select: false,
                type: SpatialQueryType.RECTANGLE
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_REMOVE: {
            return {
                ...state,
                remove: state.remove + 1
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_SELECT: {
            return {
                ...state,
                select: true,
                type: null
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_CLEAN_UNUSABLE: {
            return {
                ...state,
                cleanUsable: false
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_CLEAN_USABLE: {
            return {
                ...state,
                cleanUsable: true
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_REMOVE_UNUSABLE: {
            return {
                ...state,
                removeUsable: false
            }
        }
        case SpatialQueryActionTypes.SPATIAL_QUERY_REMOVE_USABLE: {
            return {
                ...state,
                removeUsable: true
            }
        }
        default: {
            return state;
        }
    }
}